/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.prod.bom.controller;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.redragon.framework.hibernate.data.SnowFlake;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.springmvc.ControllerSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.erp.dataset.service.DatasetCommonService;
import com.erp.masterdata.common.service.MasterDataCommonService;
import com.erp.masterdata.material.dao.data.DataBox;
import com.erp.masterdata.material.dao.model.MdMaterial;
import com.erp.prod.bom.dao.model.ProdBomLine;
import com.erp.prod.bom.dao.model.ProdBomLineCO;
import com.erp.prod.bom.service.ProdBomLineService;
import com.erp.prod.craft.dao.model.ProdWorkCraftProcedure;
import com.erp.prod.craft.service.ProdWorkCraftProcedureService;
import com.erp.prod.procedure.dao.model.ProdWorkProcedure;
import com.erp.prod.procedure.service.ProdWorkProcedureService;

@Controller
@RequestMapping("/web/prodBomLine")
public class ProdBomLineWebController extends ControllerSupport{
    
    //日志处理
    private Logger logger = LoggerFactory.getLogger(ProdBomLineWebController.class);
    
    //服务层注入
    @Autowired
    private ProdBomLineService prodBomLineService;
    @Autowired
    private MasterDataCommonService masterDataCommonService;
    @Autowired
    private DatasetCommonService datasetCommonService;
    @Autowired
    private ProdWorkCraftProcedureService prodWorkCraftProcedureService;
    @Autowired
    private ProdWorkProcedureService prodWorkProcedureService;
    
    @Override
    public String getExceptionRedirectUrl() {
        return this.getFormatRedirectUrl("getProdBomLineList");
    }
    
    
    
    /**
     * 
     * @description 查询数据列表
     * @date 2020-11-13 16:07:55
     * @author 
     * @param pages
     * @param prodBomLineCO
     * @param model
     * @return String
     *
     */
    @RequestMapping("getProdBomLineList")
    public String getProdBomLineList(Pages pages, ProdBomLineCO prodBomLineCO, Model model) {
        //分页查询数据
        if(pages.getPage()==0) {
            pages.setPage(1);
        }
        pages.setMax(100);
        
        //分页查询数据
        List<ProdBomLine> prodBomLineList = this.prodBomLineService.getBomLineListByBomHeadCode(pages, prodBomLineCO);
        
        //物料Map
        Map<String, String> materialMap = this.masterDataCommonService.getMaterialForMaterialMap();
        
        //循环获取物料信息
        for(ProdBomLine prodBomLine: prodBomLineList) {
            MdMaterial mdMaterial = this.masterDataCommonService.getMdMaterialInfoCache(prodBomLine.getMaterialCode());
            prodBomLine.setMaterialUnit(this.datasetCommonService.getMaterialUnit().get(mdMaterial.getMaterialUnit()));
            prodBomLine.setMaterialStandard(mdMaterial.getStandard());
            
            //循环获取工序名称
            ProdWorkProcedure prodWorkProcedure = this.prodWorkProcedureService.getDataObject(prodBomLine.getProcedureCode());
            if(prodWorkProcedure!=null) {
                prodBomLine.setProcedureName(prodWorkProcedure.getProcedureName());
            }
        }
        
        //页面属性设置
        model.addAttribute("prodBomLineList", prodBomLineList);
        model.addAttribute("pages", pages);
        model.addAttribute("materialMap", materialMap);
        
        return "prodBom/tab/prodBomLineTab";
    }
    
    
    
    /**
     * 
     * @description 查询单条数据
     * @date 2020-11-13 16:07:55
     * @author 
     * @param prodBomLine
     * @param model
     * @return String
     *
     */
    @RequestMapping("getProdBomLine")
    public String getProdBomLine(ProdBomLine prodBomLine, String parentMaterialCode, Model model) {
        //查询要编辑的数据
        if(prodBomLine!=null&&prodBomLine.getBomLineId()!=null) {
            prodBomLine = this.prodBomLineService.getDataObject(prodBomLine.getBomLineId());
            //获取物料信息
            MdMaterial mdMaterial = this.masterDataCommonService.getMdMaterialInfoCache(prodBomLine.getMaterialCode());
            prodBomLine.setMaterialUnit(this.datasetCommonService.getMaterialUnit().get(mdMaterial.getMaterialUnit()));
            prodBomLine.setMaterialStandard(mdMaterial.getStandard());
            prodBomLine.setBomProperty(DataBox.getBomPropertyMap().get(mdMaterial.getBomProperty()));
        }
        
        //获取头物料可选工序
        if(StringUtils.isNotBlank(parentMaterialCode)) {
            List<ProdWorkCraftProcedure> procedureList = this.prodWorkCraftProcedureService.getProcedureListByMaterialCode(parentMaterialCode);
            //循环获取工序名称
            for(ProdWorkCraftProcedure prodWorkCraftProcedure: procedureList) {
                ProdWorkProcedure prodWorkProcedure = this.prodWorkProcedureService.getDataObject(prodWorkCraftProcedure.getProcedureCode());
                if(prodWorkProcedure!=null) {
                    prodWorkCraftProcedure.setProcedureName(prodWorkProcedure.getProcedureName());
                }
            }
            model.addAttribute("procedureList", procedureList);
        }
        
        //物料Map
        Map<String, String> materialMap = this.masterDataCommonService.getMaterialForMaterialMap();
        
        //页面属性设置
        model.addAttribute("prodBomLine", prodBomLine);
        model.addAttribute("materialMap", materialMap);
        
        return "prodBom/pop/addLineModal";
    }
    
    
    
    /**
     * 
     * @description 编辑数据
     * @date 2020-11-13 16:07:55
     * @author 
     * @param prodBomLine
     * @param bindingResult
     * @param model
     * @return String
     *
     */
    @RequestMapping("editProdBomLine")
    @ResponseBody
    public String editProdBomLine(@Valid ProdBomLine prodBomLine, BindingResult bindingResult, Model model, RedirectAttributes attr) {
        try {
            //参数校验
            /*
            Map<String, String> errorMap = this.validateParameters(bindingResult, model);
            if(errorMap.size()>0) {
                return "forward:getProdBomLine";
            }
            */
            
            //对当前编辑的对象初始化默认的字段
            if(prodBomLine.getBomLineId()==null) {
                prodBomLine.setBomLineCode(SnowFlake.getConcurrentId().toString());
            }
            
            //保存编辑的数据
            this.prodBomLineService.insertOrUpdateDataObject(prodBomLine);
            
            return "{\"result\":\"success\"}";
        }catch(Exception e) {
            return "{\"result\":\"error\"}";
        }
    }
    
    
    /**
     * 
     * @description 删除数据
     * @date 2020-11-13 16:07:55
     * @author 
     * @param prodBomLine
     * @return String
     *
     */
    @RequestMapping("deleteProdBomLine")
    @ResponseBody
    public String deleteProdBomLine(ProdBomLine prodBomLine, RedirectAttributes attr) {
        try {
            //删除数据前验证数据
            if(prodBomLine!=null&&prodBomLine.getBomLineId()!=null) {
                //删除数据
                this.prodBomLineService.deleteDataObject(prodBomLine);
            }
            
            return "{\"result\":\"success\"}";
        }catch(Exception e) {
            return "{\"result\":\"error\"}";
        }
    }
}
